<?php

namespace yifang\model\concern;

use yifang\db\BaseQuery as Query;

/**
 * 虚拟模型
 */
trait Virtual
{
    /**
     * 获取当前模型的数据库查询对象
     * @return Query
     */
    public function db($scope = [])
    {
        throw new \Exception('virtual model not support db query');
    }

    /**
     * 获取字段类型信息
     * @access public
     * @param string $field 字段名
     * @return string|null
     */
    public function getFieldType($field)
    {}

    /**
     * 保存当前数据对象
     * @return bool
     */
    public function save(array $data = [], $sequence = null)
    {
        // 数据对象赋值
        $this->setAttrs($data);
        if ($this->isEmpty() || false === $this->trigger('BeforeWrite')) {
            return false;
        }
        // 写入回调
        $this->trigger('AfterWrite');
        $this->exists(true);
        return true;
    }

    /**
     * 删除当前的记录
     * @access public
     * @return bool
     */
    public function delete()
    {
        if (!$this->isExists() || $this->isEmpty() || false === $this->trigger('BeforeDelete')) {
            return false;
        }
        // 关联删除
        if (!empty($this->relationWrite)) {
            $this->autoRelationDelete();
        }
        $this->trigger('AfterDelete');
        $this->exists(false);
        return true;
    }
}
